//
//  PageContentView.swift
//  StudyDY
//
//  Created by yinchangming on 2018/7/3.
//  Copyright © 2018年 yinchangming. All rights reserved.
//

import UIKit
protocol PageContentViewDelegate :class {
    func pageChageWithProgress(progress:CGFloat,currentPage:Int ,mubiaoPage:Int)
}
class PageContentView : UIView {
    let CELLID="MYCELLID"
    var vcs:[UIViewController]
    private  weak var parentVc:UIViewController?
    private  var  startOffsetX:CGFloat=0.0
     weak var delegate:PageContentViewDelegate?
    lazy var collectionUI={ () -> UICollectionView in
        // 1 封装conection 布局
        var conllectionLayout=UICollectionViewFlowLayout()
        conllectionLayout.itemSize=self.bounds.size
        conllectionLayout.minimumLineSpacing=0 //行距
        conllectionLayout.minimumInteritemSpacing=0//间隔间距
        conllectionLayout.scrollDirection = .horizontal
       
        // 2 创建 UICollectionView
        
        var collectionView=UICollectionView(frame:CGRect(x: 0, y: 0, width: self.bounds.size.width, height: self.bounds.size.height), collectionViewLayout: conllectionLayout)
        collectionView.bounces=false//页数第一页最后一页不要反弹效果
        collectionView.showsHorizontalScrollIndicator=false
        collectionView.isPagingEnabled=true
        collectionView.dataSource=self
        collectionView.delegate=self
        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: CELLID)
        return collectionView
        
    }()
 
    init(frame: CGRect,vcs:[UIViewController],parentVc:UIViewController) {
        self.vcs=vcs
        self.parentVc=parentVc
        super.init(frame: frame)
        updateUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has not been implemented")
    }
}

extension PageContentView{
    
    private func updateUI(){
        //将所有 子vc 添加到父类
        for (_ , vc ) in vcs.enumerated(){
            parentVc?.addChildViewController(vc)
        }
        addSubview(collectionUI)
    }
}
//封装数据
extension PageContentView : UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return vcs.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell =   collectionView.dequeueReusableCell(withReuseIdentifier: CELLID, for: indexPath)
        let childVc=vcs[indexPath.item]
        for view in cell.contentView.subviews{
            view.removeFromSuperview()
        }
        childVc.view.frame=cell.contentView.bounds
        cell.contentView.addSubview(childVc.view)
        return cell
    }
}
extension PageContentView :UICollectionViewDelegate{
   
    //开始滑动
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        startOffsetX=scrollView.contentOffset.x
    }
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let currentOffsetX=scrollView.contentOffset.x
        //获取滑动比例
        var  progress:CGFloat=0//滑动进度
        var mubiaoPage=0
        var currentPage=0
        if(currentOffsetX>=startOffsetX){
          progress =  currentOffsetX / kScreenW - floor(  currentOffsetX / kScreenW )
            currentPage = Int(  currentOffsetX / kScreenW )
            mubiaoPage=currentPage+1
            
            
            if(currentPage>=vcs.count-1){
                progress=1
                mubiaoPage=currentPage
            }
            
        }else{
              progress = 1 - (currentOffsetX / kScreenW - floor(  currentOffsetX / kScreenW ))
            mubiaoPage =  Int(  currentOffsetX / kScreenW )
            currentPage=mubiaoPage+1
            if(mubiaoPage==0){
                currentPage=0
            }
           
        }
      
        delegate?.pageChageWithProgress(progress: progress, currentPage: currentPage, mubiaoPage: mubiaoPage)
    }
}
//对外暴露的方法
extension  PageContentView{
    //根据titleview选择currentIndex 显示对应的页
    func setContentPageByTitleView(currentIndex:Int){
        let currentX=CGFloat( currentIndex) * kScreenW
        collectionUI.setContentOffset(CGPoint(x: currentX, y: 0), animated: false)
    }
}
